package com.rex.saas.controller;

import com.rex.saas.bean.BaseResponse;
import com.rex.saas.bean.ResultCode;
import com.rex.saas.bean.request.CategoryAddRequest;
import com.rex.saas.bean.response.CategroyResponse;
import com.rex.saas.service.CategoryService;
import com.rex.saas.service.RexolarApiClient;
import com.rex.saas.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName CategoryController
 * @Description:
 * @Author: zhusiyu
 * @CreateDate: 2024/12/18 10:55	//创建时间
 */
@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    RexolarApiClient rexolarApiClient;

    @GetMapping("/list")
    public BaseResponse list(HttpServletRequest request){
        String token = request.getHeader("token");
        String tenantId = JwtUtils.getTenantId(token);
        String tenantToken = rexolarApiClient.getTenantToken(tenantId);
        List<CategroyResponse> list = categoryService.list(tenantToken);
        return BaseResponse.ok(list);
    }

    @GetMapping("/details")
    public BaseResponse details(HttpServletRequest request, @RequestParam String categoryId){
        log.info("details categoryId:{}", categoryId);
        String token = request.getHeader("token");
        String tenantId = JwtUtils.getTenantId(token);
        String tenantToken = rexolarApiClient.getTenantToken(tenantId);
        CategroyResponse categroyResponse = categoryService.details(tenantToken, categoryId);
        return BaseResponse.ok(categroyResponse);
    }

    @GetMapping("/getDetailsByName")
    public BaseResponse getDetailsByName(HttpServletRequest request, @RequestParam String name){
        log.info("getDetailsByName deviceName:{}", name);
        String token = request.getHeader("token");
        CategroyResponse categroyResponse = categoryService.getDetailsByName(token, name);
        return BaseResponse.ok(categroyResponse);
    }

    @PostMapping("/add")
    public BaseResponse add(HttpServletRequest request, @RequestBody CategoryAddRequest categoryAddRequest){
        String token = request.getHeader("token");
        String tenantId = JwtUtils.getTenantId(token);
        if(StringUtils.isEmpty(tenantId)){
            log.error("add tenantId is null,spaceInfoId:{}", tenantId);
            return BaseResponse.error("token异常", ResultCode.PARAM_ERROR);
        }
        if(StringUtils.isEmpty(categoryAddRequest.getCategoryName())){
            log.error("add categoryName is null,categoryName:{}", categoryAddRequest.getCategoryName());
            return BaseResponse.error("分类名称不能为空", ResultCode.PARAM_ERROR);
        }

        List<CategroyResponse> list = categoryService.list(token);
        for (CategroyResponse categroyResponse : list) {
            if(categroyResponse.getCategoryName().equals(categoryAddRequest.getCategoryName())){
                log.error("add categoryName is exist, addDeviceRequest:{}", categoryAddRequest);
                return BaseResponse.error("分类名称已存在", ResultCode.PARAM_ERROR);
            }
        }

        String result = categoryService.add(token, categoryAddRequest);
        if(StringUtils.isEmpty(result)){
            log.error("add category fail, categoryAddRequest:{}", categoryAddRequest);
            return BaseResponse.error("添加失败", ResultCode.PARAM_ERROR);
        }
        return BaseResponse.ok(result);
    }
}
